using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace iTron.Model
{
    class GUI
    {
        private const int SPRITE_HEIGHT = 8, SPRITE_WIDTH = 8;
        private const float SPRITE_FLOAT_ROTATION = -1.570796f; // = 90^o
        private ContentManager cm;
        private GameTime gameTime;
        private Texture2D leftBtn, rightBtn, boostBtn, brakeBtn, wall;
        private Vector2 leftBtnPos, rightBtnPos, boostBtnPos, brakeBtnPos;

        public GUI(ContentManager cm)
        {
            this.cm = cm;
            // Adding left button texture
            leftBtn = cm.Load<Texture2D>("leftBtn");
            leftBtnPos = new Vector2(720, 240);

            // Adding right button texture
            rightBtn = cm.Load<Texture2D>("rightBtn");
            rightBtnPos = new Vector2(720, 120);

            // Adding boost button texture
            boostBtn = cm.Load<Texture2D>("boostBtn");
            boostBtnPos = new Vector2(720, 360);

            // Adding brake button texture
            brakeBtn = cm.Load<Texture2D>("brakeBtn");
            brakeBtnPos = new Vector2(720, 480);

            // Add wall texture
            wall = cm.Load<Texture2D>("wall");

            //TODO: ... finish
        }

        public int getTouchInput()
        {
            TouchCollection touches = TouchPanel.GetState();
            if (touches.Count > 0)
            {

                Rectangle leftRect = leftBtn.Bounds;
                Rectangle rightRect = rightBtn.Bounds;
                Rectangle boostRect = boostBtn.Bounds;
                Rectangle brakeRect = brakeBtn.Bounds;

                List<TouchLocation> loc = touches.ToList();
                Vector2 touchPosition = loc[0].Position;

                //Left
                if (touchPosition.X >= leftBtnPos.X && touchPosition.X <= (leftBtnPos.X + leftRect.Height)
                    && touchPosition.Y >= (leftBtnPos.Y - leftRect.Width) && touchPosition.Y <= leftBtnPos.Y)
                {
                    return Utils.BUTTON_LEFT;
                }
                //Right
                if (touchPosition.X >= rightBtnPos.X && touchPosition.X <= (rightBtnPos.X + rightRect.Height)
                    && touchPosition.Y >= (rightBtnPos.Y - rightRect.Width) && touchPosition.Y <= rightBtnPos.Y)
                {
                    return Utils.BUTTON_RIGHT;
                }
                //Boost
                if (touchPosition.X >= boostBtnPos.X && touchPosition.X <= (boostBtnPos.X + boostRect.Height)
                    && touchPosition.Y >= (boostBtnPos.Y - boostRect.Width) && touchPosition.Y <= boostBtnPos.Y)
                {
                    return Utils.BUTTON_BOOST;
                }
                //Brake
                if (touchPosition.X >= brakeBtnPos.X && touchPosition.X <= (brakeBtnPos.X + brakeRect.Height)
                    && touchPosition.Y >= (brakeBtnPos.Y - brakeRect.Width) && touchPosition.Y <= brakeBtnPos.Y)
                {
                    return Utils.BUTTON_BRAKE;
                }

            }
            return 0;
        }

        //public void draw(Microsoft.Xna.Framework.Graphics.SpriteBatch batch, bool animate)
        public void draw(Microsoft.Xna.Framework.Graphics.SpriteBatch batch)
        {
            // Add buttons
            batch.Draw(leftBtn, leftBtnPos, null, Color.WhiteSmoke, SPRITE_FLOAT_ROTATION, new Vector2(0f, 0f), 1.0f, SpriteEffects.None, 1.0f);
            batch.Draw(rightBtn, rightBtnPos, null, Color.WhiteSmoke, SPRITE_FLOAT_ROTATION, new Vector2(0f, 0f), 1.0f, SpriteEffects.None, 1.0f);
            batch.Draw(boostBtn, boostBtnPos, null, Color.WhiteSmoke, SPRITE_FLOAT_ROTATION, new Vector2(0f, 0f), 1.0f, SpriteEffects.None, 1.0f);
            batch.Draw(brakeBtn, brakeBtnPos, null, Color.WhiteSmoke, SPRITE_FLOAT_ROTATION, new Vector2(0f, 0f), 1.0f, SpriteEffects.None, 1.0f);

            // Draw the wall
            Texture2D obj = wall;
            Rectangle wallRect = new Rectangle(0, 0, SPRITE_WIDTH, SPRITE_HEIGHT);
            for (int i = 0; i < 60; i++)
            {
                for (int j = 0; j < 60; j++)
                {
                    if (i == 0)
                    {
                        Vector2 pos = new Vector2(0, j * 8);
                        batch.Draw(obj, pos, wallRect, Color.WhiteSmoke, SPRITE_FLOAT_ROTATION, new Vector2(+8f, -92f), 1.0f, SpriteEffects.None, 1.0f);
                    }
                    if (i == 59)
                    {
                        Vector2 pos = new Vector2(i * 8, j * 8);
                        batch.Draw(obj, pos, wallRect, Color.WhiteSmoke, SPRITE_FLOAT_ROTATION, new Vector2(+8f, -92f), 1.0f, SpriteEffects.None, 1.0f);
                    }
                    if (j == 0)
                    {
                        Vector2 pos = new Vector2(i * 8, 0);
                        batch.Draw(obj, pos, wallRect, Color.WhiteSmoke, SPRITE_FLOAT_ROTATION, new Vector2(+8f, -92f), 1.0f, SpriteEffects.None, 1.0f);
                    }
                    if (j == 59)
                    {
                        Vector2 pos = new Vector2(i * 8, j * 8);
                        batch.Draw(obj, pos, wallRect, Color.WhiteSmoke, SPRITE_FLOAT_ROTATION, new Vector2(+8f, -92f), 1.0f, SpriteEffects.None, 1.0f);
                    }
                }
            }
        }

        public void update(Microsoft.Xna.Framework.GameTime time)
        {
            this.gameTime = time;

            // TODO:
        }
    }
}
